// pages/my/withdraw/index.js
const app = getApp()
const {
  baseURL,
  ip
} = require('../../../fetch/http')
const api = require('../../../fetch/api')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseURL: baseURL,
    ip:ip,
    alipayAccount: '',
    alipayName: '',
    show: false,
    money: '',
    type: 1, // 2 微信 1 支付宝(默认)
    accountInfo:"",
    bankName:"",
    bannkAccount:"",
    bankstatus:"",
    wxqr:"",
    name:"",
    aliqr:""
  },
  upload(){
    console.log(122112)
    wx.navigateTo({
      url: "/pages/accountManage/manage/index?id="+2,
    })
  },

  upload2(){
    wx.navigateTo({
      url: "/pages/accountManage/manage/index?id="+1,
    })
  },
  switchType(e) {
    const {
      index
    } = app.dataSet(e)
    this.setData({
      type: index
    })
    this.close()
  },
  inputAlipayAccount(e) {
    this.setData({
      alipayAccount: e.detail.value
    })
  },
  inputAlipayName(e) {
    this.setData({
      alipayName: e.detail.value
    })
  },
  inputbankName(e){
    this.setData({
      bankName: e.detail.value
    })
  },
  inputbannkAccount(e){
    this.setData({
      bannkAccount: e.detail.value
    })
  },

  inputbankstatus(e){
    this.setData({
      bankstatus: e.detail.value
    })
  },

  inputMoney(e) {
    this.setData({
      money: e.detail.value
    })
  },
  async withdraw() {
    let {
      money,
      commission,
      type,
      alipayAccount,
      alipayName,
      bannkAccount,
      bankName,
      bankstatus,
    } = this.data
    if(this.data.type==3){
      // if(!this.data.accountInfo){
        if(!bankName){
          app.toast('请输入真实姓名')
          return
        }
        if(!bannkAccount){
          app.toast('请输入银行卡号')
          return
        }
        if(!bankstatus){
          app.toast('请输入银行名称')
          return
        }
        // || (Number(money))==0
        if (Number(money) > Number(commission)) {
          app.toast('余额不足')
          return
        }
    
        if(Number(money)<=0){
          app.toast('请输入正确金额')
          return
        }
        let res = await api.saveInfo({
          bank_name:bankName,
          card:bannkAccount,
          bank:bankstatus,
        })
        if(res.code==200){
          let res = await api.withdraw({
            type,
            money,
          })
          if (res.code == 200) {
            this.userinfo()
            this.setData({
              money: ''
            })
            wx.showToast({
              title: '申请成功',
              icon: 'success'
            })
          }
        // }
      }else{
        if (Number(money) > Number(commission)) {
          app.toast('余额不足')
          return
        }
    
        if(Number(money)<=0){
          app.toast('请输入正确金额')
          return
        }
        let res = await api.withdraw({
          type,
          money,
        })
        if (res.code == 200) {
          this.userinfo()
          this.setData({
            money: ''
          })
          wx.showToast({
            title: '申请成功',
            icon: 'success'
          })
        }
      }
      return
    }

    if (Number(money) > Number(commission)) {
      app.toast('余额不足')
      return
    }

    if(Number(money)<=0){
      app.toast('请输入正确金额')
      return
    }

    let res = await api.withdraw({
      type,
      money,
      realname: alipayName,
      account: alipayAccount
    })
    if (res.code == 200) {
      this.userinfo()
      this.setData({
        money: ''
      })
      wx.showToast({
        title: '申请成功',
        icon: 'success'
      })
    }
  },
  show() {
    this.setData({
      show: true
    })
  },
  close() {
    this.setData({
      show: false
    })
  },
  async userinfo() {
    let res = await api.userinfo()
    let {
      commission,
      alipayAccount,
    } = res.data

    this.setData({
      commission,
      alipayAccount,
   
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
   
   

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    let res = await api.userinfo()
    let {
      commission,
      alipayAccount,
      accountInfo,
    } = res.data
    this.setData({
      commission,
      money: commission,
      accountInfo:accountInfo,
      bankstatus:accountInfo.bank,
      bankName:accountInfo.bankName,
      bannkAccount:accountInfo.card
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})